# This is a_ sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import numpy as np

# ---- run Test
num: int = 0


def runTest(fun):
    global num
    print('----------------------', num)
    fun()
    num += 1
    print()


#  test arrange

def testArange():
    x = np.arange(8)
    print(x)

    print()
    x = np.arange(1, 10, 2)
    print(x)


def testLinspace():
    # 生成10个样本
    a = np.linspace(1, 10, 10)
    print(a)

    arr = np.linspace(10, 20, 5, endpoint=False)
    print("数组数值范围 ：", arr)

    x = np.linspace(1, 2, 5, retstep=True)
    print(x)


def testLogspace():
    a = np.logspace(1.0, 2.0, num=10)
    print(a)


def testSlice():
    a = np.arange(10)
    # 生成切片对象
    s = slice(2, 9, 3)  # 从索引2开始到索引9停止，间隔时间为2
    print(a[s])

    a = np.arange(10)
    b = a[2:9:2]
    print(b)

    a = np.arange(10)
    b = a[3]
    print(b)

    a = np.arange(10)
    print(a[2:])

    a = np.arange(10)
    print(a[2:5])


def test_dim_space():
    a = np.array([[1, 2, 3], [3, 4, 5], [4, 5, 6]])
    print(a)
    print('# 从[1:]索引处开始切割')
    print(a[1:])

    a = [1, 2, 3, 3, 4, 5, 4, 5, 6]
    b = np.array(a)
    c = b.reshape(3, 3)
    a = c
    print('# 返回数组的第二列')
    print(a[..., 1])
    print('# 返回数组的第二行')
    print(a[1, ...])
    print('# 返回第二列后的所有项')
    print(a[..., 1:])


def test_advance_slice():
    # 创建二维数组
    x = np.array([[1, 2], [3, 4], [5, 6]])
    # [0,1,2]代表行索引;[0,1,0]代表列索引
    y = x[[0, 1, 2], [0, 1, 0]]
    print(y)

    print()
    b = np.array([[0, 1, 2],
                  [3, 4, 5],
                  [6, 7, 8],
                  [9, 10, 11]])
    r = np.array([[0, 0], [3, 3]])
    c = np.array([[0, 2], [0, 2]])
    # 获取四个角的元素
    c = b[r, c]
    print(c)


def test_adv_slice2():
    d = np.array([[0, 1, 2],
                  [3, 4, 5],
                  [6, 7, 8],
                  [9, 10, 11]])
    # 对行列分别进行切片
    e = d[1:4, 1:3]
    print(e)
    # 行使用基础索引，对列使用高级索引
    f = d[1:4, [1, 2]]
    # 显示切片后结果
    print(f)
    # 对行使用省略号
    h = d[..., 1:]
    print(h)


def test_bool_slice():
    x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
    print(x[x > 6])

    x = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])
    print(x[x > 6])

    a = np.array([1, 2 + 6j, 5, 3.5 + 5j])
    print(a[np.iscomplex(a)])


def test_fancy_index():
    x = np.arange(32).reshape((8, 4))
    # 分别对应 第4行数据、第2行数据、第1行数据、第7行数据项
    print(x[[4, 2, 1, 7]])
    print()
    print(x[[-4, -2, -1, -7]])


def test_fancy_index2():
    x = np.arange(32).reshape((8, 4))
    print(x[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])])
    print(x[1, 0], x[1, 3], x[1, 1], x[1, 2])


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    runTest(testArange)
    runTest(testLinspace)
    runTest(testLogspace)
    runTest(testSlice)
    runTest(test_dim_space)
    print()
    runTest(test_advance_slice)
    runTest(test_adv_slice2)
    runTest(test_bool_slice)
    runTest(test_fancy_index)
    runTest(test_fancy_index2)
